package com.wap.framework.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wap.framework.model.Advertisement;
import com.wap.framework.service.AdService;


public class AdManagerAction extends DispatchAction {
	private AdService adService;

	public void setAdService(AdService adService) {
		this.adService = adService;
	}

	/**
	 * list all advertisement
	 */
	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String currentPage = request.getParameter("currentPage");
		if(currentPage == null || "".equals(currentPage)) {
			currentPage = "1";
		}
		
		List<Object> result = adService.listAdvertisement(Integer.valueOf(currentPage));		
		request.setAttribute("ads", (List<Advertisement>)result.get(0));
		request.setAttribute("pageHelper", result.get(1));
		
		return mapping.findForward("fwd_mgr_Ad");
	}

	/**
	 * Add advertisement
	 */
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String title 	= request.getParameter("title");
		String href 	= request.getParameter("href");
		String source 	= request.getParameter("source");
		String type 	= request.getParameter("type");
		String status 	= request.getParameter("status");

		Advertisement ad = new Advertisement();
		ad.setTitle(title);
		ad.setHref(href);
		ad.setSource(source);
		ad.setType(Integer.valueOf(type));
		ad.setStatus(Integer.valueOf(status));

		adService.insertAdvertisement(ad);

		return list(mapping, form, request, response);
	}

	/**
	 * Delete ad
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] ids = request.getParameterValues("id");
		int[] adIds = new int[ids.length];
		for (int i = 0; i < ids.length; i++) {
			adIds[i] = Integer.valueOf(ids[i]);
		}
		this.adService.deleteAdvertisement(adIds);
		return list(mapping, form, request, response);
	}

	/**
	 * Edit ad
	 */
	@SuppressWarnings("unchecked")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String adId = request.getParameter("adId");
		Advertisement ad = this.adService.editAdvertisement(Integer
				.valueOf(adId));
		request.setAttribute("ad", ad);
		return mapping.findForward("fwd_edit_Ad");
	}

	/**
	 * Update ad
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String adId 	= request.getParameter("adId");
		String title 	= request.getParameter("title");
		String href 	= request.getParameter("href");
		String source 	= request.getParameter("source");
		String type 	= request.getParameter("type");
		String status 	= request.getParameter("status");

		Advertisement ad = new Advertisement();
		ad.setId(Integer.valueOf(adId));
		ad.setTitle(title);
		ad.setHref(href);
		ad.setSource(source);
		ad.setType(Integer.valueOf(type));
		ad.setStatus(Integer.valueOf(status));

		adService.updateAdvertisement(ad);

		return list(mapping, form, request, response);
	}
}
